package coml.jiyun.hp.wanandroid.ui.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.youth.banner.Banner;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;
import java.util.List;

import coml.jiyun.hp.wanandroid.R;
import coml.jiyun.hp.wanandroid.bean.BannerBean;
import coml.jiyun.hp.wanandroid.bean.HomeBean;

public class RlvHomeAdapter extends RecyclerView.Adapter{

    private Context context;
    private List<BannerBean.DataBean> bannerList = new ArrayList<>();
    private List<HomeBean.DataBean> dataBeans=new ArrayList<>() ;
    private int ban = 0;
    private int rlv = 1;

    public RlvHomeAdapter(Context context) {
        this.context = context;
    }

    public void setBannerList(List<BannerBean.DataBean> bannerList) {
        this.bannerList.addAll(bannerList);
    }

    public void setDataBeans(List<HomeBean.DataBean> dataBeans) {
        this.dataBeans.addAll(dataBeans);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == ban){
            View inflate = LayoutInflater.from(context).inflate(R.layout.item_home_banner, parent, false);
            return new BannerHolder(inflate);
        }else {
            View inflate = LayoutInflater.from(context).inflate(R.layout.item_home_rlv, parent, false);
            return new RlvHolder(inflate);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
        int itemViewType = holder.getItemViewType();
        if (itemViewType == ban){
            final BannerHolder bannerHolder = (BannerHolder) holder;
            ArrayList<String> list = new ArrayList<>();
            for (int i = 0; i < bannerList.size(); i++) {
                list.add(bannerList.get(i).getImagePath());
            }
            bannerHolder.mBan.setImages(list)
                    .setImageLoader(new ImageLoader() {
                        @Override
                        public void displayImage(Context context, Object path, ImageView imageView) {

                            Glide.with(context).load(path).into(imageView);
                        }
                    }).start();
        }else {
            RlvHolder rlvHolder = (RlvHolder) holder;
            HomeBean.DataBean datasBean = dataBeans.get(position);
            rlvHolder.mTv1.setText(datasBean.getAuthor());
            rlvHolder.mTv2.setText(datasBean.getNiceDate());
            rlvHolder.mTv3.setText(datasBean.getTitle());
            rlvHolder.mTv4.setText(datasBean.getChapterName());
        }
    }


    @Override
    public int getItemViewType(int position) {
        if (position == 0 && bannerList.size() >0){
            return ban;
        }else {
            return rlv;
        }
    }

    @Override
    public int getItemCount() {
        return dataBeans.size();
    }

    public class RlvHolder extends RecyclerView.ViewHolder {

        TextView mTv1;
        TextView mTv2;
        TextView mTv3;
        TextView mTv4;
        ImageView mImg;

        public RlvHolder(View itemView) {
            super(itemView);

            mTv1 = itemView.findViewById(R.id.item_home_tv1);
            mTv2 = itemView.findViewById(R.id.item_home_tv2);
            mTv3 = itemView.findViewById(R.id.item_home_tv3);
            mTv4 = itemView.findViewById(R.id.item_home_tv4);
            mImg = itemView.findViewById(R.id.item_home_img);

        }
    }

    public class BannerHolder extends RecyclerView.ViewHolder {

        Banner mBan;

        public BannerHolder(View itemView) {
            super(itemView);

            mBan = itemView.findViewById(R.id.item_banner_banner);

        }
    }
}
